import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';

import '../../common/index.dart';
import 'index.dart';

class ChatListPage extends GetView<ChatListController> {
  const ChatListPage({super.key});

  // 主视图
  Widget _buildView() {
    return Column(
      children: [
        SizedBox(height: 45.h),
        Row(
          children: [
            SizedBox(width: 24.w),
            GestureDetector(
              onTap: () => RouteUtil.pop(),
              child: Container(
                height: 40.h,
                width: 40.w,
                padding: EdgeInsets.all(10.r),
                decoration: BoxDecoration(
                  color: Color(0xFFCDFF5F),
                  borderRadius: BorderRadius.circular(10.r),
                ),
                child: SvgPicture.asset(
                  AssetsImages.back,
                  width: 20.w,
                  height: 20.h,
                ),
              ),
            ),
            Spacer(),
            Text(
              "Message",
              style: TextStyle(
                fontSize: 24.sp,
                fontWeight: FontWeight.w900,
                color: Color(0xFFCDFF5F),
              ),
            ),
            Spacer(),
            SizedBox(width: 64.w),
          ],
        ),

        SizedBox(height: 20.h),

        // 聊天列表
        Expanded(
          child: Obx(
            () => controller.chat_list_items.isEmpty
                ? _buildEmptyView()
                : _buildChatList(),
          ),
        ),
      ],
    );
  }

  // 空视图
  Widget _buildEmptyView() {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(height: 100.h),
          Text(
            "No messages yet",
            style: TextStyle(color: Colors.white70, fontSize: 16.sp),
          ),
          SizedBox(height: 20.h),
          Text(
            "Start chatting with plant lovers",
            style: TextStyle(color: Colors.white54, fontSize: 14.sp),
          ),
        ],
      ),
    );
  }

  // 聊天列表
  Widget _buildChatList() {
    return ListView.builder(
      itemCount: controller.chat_list_items.length,
      padding: EdgeInsets.symmetric(horizontal: 24.w),
      itemBuilder: (context, index) {
        final item = controller.chat_list_items[index];
        return _buildChatListItem(item);
      },
    );
  }

  // 单个聊天项
  Widget _buildChatListItem(ChatListItem item) {
    return GestureDetector(
      onTap: () => controller.navigate_to_chat_detail(item.user_id),
      child: Container(
        margin: EdgeInsets.only(bottom: 16.h),
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Colors.white.withOpacity(0.2),
              width: 0.5,
            ),
          ),
        ),
        padding: EdgeInsets.only(bottom: 16.h),
        child: Row(
          children: [
            // 头像
            Container(
              width: 50.w,
              height: 50.h,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                image: DecorationImage(
                  image: item.avatar.isNotEmpty
                      ? ImagePath().get_avatar_image(item.avatar)
                      : AssetImage(AssetsImages.defaultAvatar),
                  fit: BoxFit.cover,
                ),
              ),
            ),
            SizedBox(width: 12.w),

            // 用户信息和消息预览
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        item.name,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      Text(
                        item.formatted_time,
                        style: TextStyle(
                          color: Colors.white60,
                          fontSize: 12.sp,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 4.h),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Expanded(
                        child: Text(
                          item.last_message,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            color: Colors.white70,
                            fontSize: 14.sp,
                          ),
                        ),
                      ),
                      if (item.unread_count > 0)
                        Container(
                          padding: EdgeInsets.symmetric(
                            horizontal: 8.w,
                            vertical: 2.h,
                          ),
                          decoration: BoxDecoration(
                            color: Color(0xFFCDFF5F),
                            borderRadius: BorderRadius.circular(10.r),
                          ),
                          child: Text(
                            "${item.unread_count}",
                            style: TextStyle(
                              color: Colors.black,
                              fontSize: 12.sp,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ChatListController>(
      init: ChatListController(),
      id: "chat_list",
      builder: (_) {
        return Scaffold(
          body: Container(
            width: double.infinity,
            height: double.infinity,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage(AssetsImages.bg),
                fit: BoxFit.cover,
              ),
            ),
            child: SafeArea(top: false, bottom: false, child: _buildView()),
          ),
        );
      },
    );
  }
}
